<?php
/**
 * Auto Complete on a contact
 *
 * @package Hive\Contact
 * @author Xavier VILAIN
 */

use Fox\SearchEngine;
use Hive\Ressource;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Connection;

$xview = Xview::instance();
$final = array();
$term = utf8_decode(Xview::instance()->val('term'));
$term = strtolower(SearchEngine::cleanupContent($term));
$term = str_replace(array('-',' '),'%',$term);
$terms = '%'.$term.'%';
$companid = $xview->val('companid');
$data = array();
$infos = Fox\Dao::infos(Ressource::className());
$query = $infos->handle()->createQueryBuilder();
$query->select('t.ressource_id as id','t.last_name','t.first_name','xp.jobtitle')
		->from($infos->tableName(),'t')
		->leftJoin('t','ressource_experiences','xp','t.ressource_id = xp.ressource_id AND xp.current = 1')
		->setMaxResults(10);
$fragment = new Fox\Dao\SqlFragment('( t.last_name LIKE ? OR first_name LIKE ?  ) AND loekz = ?',array($terms,$terms,0));
$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $data) {
	$label = $data['first_name'].' '.$data['last_name'].' - '.$data['jobtitle'];
	$final[] = array('id' => $data['id'],'label' => $label);
}
echo Fox\Utilities::jsonUTF8Encode($final);